//
//  TypeCell.m
//  MyTrip
//
//  Created by zhang on 15/8/27.
//  Copyright (c) 2015年 张益. All rights reserved.
//

#import "TypeCell.h"
#import "PackCell.h"
#import "LookTypeHeaderView.h"
#import "PackDetailController.h"


@interface TypeCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>



@end

@implementation TypeCell

- (void)dealloc
{
    [_typeCollection release];
    [_typeArray release];
    [super dealloc];
}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.typeArray = [NSMutableArray array];
        [self createCollection];
        
    }
    return self;
}

- (void)createCollection{
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(self.frame.size.width / 3 - 10 * OffWidth, 70 * OffHeight);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(5 * OffHeight, 10 * OffWidth, 5 * OffHeight, 10 * OffWidth);

    
    self.typeCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:flowLayout];
    _typeCollection.delegate = self;
    _typeCollection.dataSource = self;
    [_typeCollection registerClass:[PackCell class] forCellWithReuseIdentifier:@"packcell"];
    [_typeCollection registerClass:[LookTypeHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [_typeCollection setBackgroundColor:[UIColor whiteColor]];
    [self.contentView addSubview:_typeCollection];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return _typeArray.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSDictionary *dic = [_typeArray objectAtIndex:section];
    NSArray *guides = [dic objectForKey:@"guides"];
    return guides.count;
}


- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = [_typeArray objectAtIndex:indexPath.section];
    NSString *name = [dic objectForKey:@"name"];
    LookTypeHeaderView *reuseview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
    
    if (kind == UICollectionElementKindSectionHeader) {
        reuseview.nameLabel.text = name;
        reuseview.layer.opacity = 0.5;
    }
    return reuseview;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(0, 30 * OffHeight);
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    PackCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"packcell" forIndexPath:indexPath];
    NSDictionary *dic = [_typeArray objectAtIndex:indexPath.section];
    NSArray *guides = [dic objectForKey:@"guides"];
    NSDictionary *gDic = [guides objectAtIndex:indexPath.item];
//    UIColor *color = [UIColor colorWithRed:arc4random() % 256 / 256.0 green:arc4random() %256 / 256.0 blue:arc4random() % 256 / 256.0 alpha:1];
//    [cell setBackgroundColor:color];
    [cell setValueWithDic:gDic];
    
    return cell;
    
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//    guide_id
    NSDictionary *dic = [_typeArray objectAtIndex:indexPath.section];
    NSArray *guides = [dic objectForKey:@"guides"];
    NSDictionary *gDic = [guides objectAtIndex:indexPath.item];
    
    [self.delegate clickIndex:indexPath WithIdNum:[[gDic objectForKey:@"guide_id"] integerValue]];
}





@end
